var page = require('webpage').create(),
    args = require('system').args,
    siteurl = 'http://10.51.3.2/';

page.viewportSize = {width: 1024, height: 800};
var username = args[1];
var password = args[2];

var count = 1;
page.onLoadFinished = function(status) {
  if(status != 'success') {
    console.log('Unable to open ' + page.url);
    phantom.exit();
  }
  console.log("onLoadFinished: " + page.url);
  if(page.url.indexOf('http://10.51.3.2/eportal/index.jsp') === 0) {
    console.log('start login');
    login();
  }
  if(page.url.indexOf('http://10.51.3.2/eportal/success.jsp') === 0) {
    console.log('success.');
    phantom.exit();
  }
};

//输出浏览器的控制台输出，调试时很有帮助。
page.onConsoleMessage = function(msg) {
  console.log('CONSOLE: ' + msg);
};

page.open(siteurl);

function login() {
  page.evaluate(function(uname, pass) {
    document.getElementById('username').value = uname;
    document.getElementById('pwd').value = pass;
    doauthen();
  }, username, password);
}
